﻿using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.DocModel.APIParam;
using HCQ2_Model.VRDD.InputDto;
using HCQ2_Model.VRDD.InputDto._0427;
using HCQ2_Model.VRDD.OutputDto._0427;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace HCQ2_BLL
{
    public partial class DD_FriendApplyBLL : IDD_FriendApplyBLL
    {
        /// <summary>
        ///  获取好友申请列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<List<OutApplyFriend>> GetApplyFirend(DDBaseUser param)
        {
            List<OutApplyFriend> list = await DBSession.IDD_FriendApplyDAL.GetApplyFirend(param);
            list?.ForEach(s =>
            {
                if (!string.IsNullOrEmpty(s.user_img) && s.user_img.IndexOf("http") < 0)
                    s.user_img = s.user_img.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
            });
            return list;
        }
        /// <summary>
        ///  获取匹配手机通讯好友
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutFriendPhone> GetFriendByPhone(QueryFriendPhone param)
        {
            List<OutFriendPhone> list = DBSession.IDD_FriendApplyDAL.GetFriendByPhone(param);
            //循环添加不存在的联系人
            param.user_phone?.ForEach(o =>
            {
                var userCount = list.Where(s => s.user_phone.Equals(o.user_phone))?.ToList().Count;
                //不存在添加
                if (userCount == null || userCount <= 0)
                    list.Add(new OutFriendPhone { user_phone = o.user_phone, user_name = o.phone_name });
            });
            list?.ForEach(s =>
            {
                var temp = param.user_phone.Where(o => o.user_phone.Equals(s.user_phone)).FirstOrDefault();
                s.phone_name = temp?.phone_name;
                if (!string.IsNullOrEmpty(s.user_img) && s.user_img.IndexOf("http") < 0)
                    s.user_img = s.user_img.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
            });
            return list;
        }
        /// <summary>
        ///  获取我的通讯录好友列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutMyFriend> GetMyFriend(DDBaseUser param)
        {
            var list = DBSession.IDD_FriendApplyDAL.GetMyFriend(param);
            list?.ForEach(s =>
            {
                if (s.fileID >0)
                {
                    var folderId = DBSession.IT_DocumentFolderRelationDAL.FindSingle(o => o.file_id == s.fileID);
                    if (folderId != null)
                    {
                        s.folderList = DBSession.IT_VRStayUserLogDAL.GetFloderAndParent(folderId.folder_id);
                        s.folderList.ForEach(o =>
                        {
                            if (!string.IsNullOrEmpty(o.folderImage))
                                o.folderImage = o.folderImage.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                        });
                    }
                }
                //处理大小写转换
                if (!string.IsNullOrEmpty(s.letter))
                    s.letter = s.letter.ToUpper();
                //处理头像
                if (!string.IsNullOrEmpty(s.user_img) && s.user_img.IndexOf("http") < 0)
                    s.user_img = s.user_img.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                //处理对方是否好友状态
                if (s.friendStatus > 0)
                    s.friendStatus = 1;
            });


            return list;
        }
        /// <summary>
        ///  获取人脉详情
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public OutConnectionDetial GetConnectionDetial(QueryConnectionDetial param, string userId, string ddId)
        {
            OutConnectionDetial result = new OutConnectionDetial();
            OutConnection obj = DBSession.IDD_FriendApplyDAL.GetOutConnectionDetial(param, userId, ddId);
            if (obj != null)
            {
                result.friendNote = obj.friendNote;
                result.friendDescribe = obj.friendDescribe;
                result.closeType = obj.closeType;
                result.closeName = obj.closeName;
                result.letter = (!string.IsNullOrEmpty(obj.letter)) ? obj.letter.ToUpper() : "";
                result.friendStatus = obj.friendStatus > 0 ? 1 : 0;
                result.isStar = obj.isStar;
                result.countFan = obj.countFan;
                result.starNum = obj.starNum;
                result.dd_line = obj.dd_line;
                result.sense = obj.sense;
                result.user_type = obj.user_type;
                result.city = obj.city;
                result.friend_num = obj.friend_num;
                result.unread_num = obj.unread_num;
                result.userid = obj.userid;
                result.gps_x = obj.gps_x;
                result.gps_y = obj.gps_y;
                result.user_note = obj.user_note;
                result.user_id = obj.user_id;
                result.user_name = obj.user_name;
                result.du_prof = obj.du_prof;
                result.du_school = obj.du_school;
                result.du_fans = obj.du_fans;
                result.count_visitor = obj.count_visitor;
                result.du_name = obj.du_name;
                //头像地址
                if (!string.IsNullOrEmpty(obj.user_img) && obj.user_img.IndexOf("http") < 0)
                    result.user_img = obj.user_img.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                if (obj.du_friend > 0)
                    //好友
                    result.du_friend = 1;
                else
                {
                    if (obj.du_friend2 > 0)
                        result.du_friend = 2;
                    else if (obj.du_friend3 > 0)
                        result.du_friend = 3;
                    else if (userId.Equals(ddId))
                        result.du_friend = 0;
                    else
                        result.du_friend = 100;
                }
                if (obj.du_friend > 0 || obj.hide_mobile > 1)
                    result.user_phone = obj.user_phone;
                //if (obj.du_friend > 0 || obj.hide_name > 1)
                //    result.du_name = obj.du_name;
                if (obj.du_friend > 0 || obj.hide_sex > 1)
                    result.du_sex = obj.du_sex;
                if (obj.du_friend > 0 || obj.hide_birthday > 1)
                    result.du_birthday = obj.du_birthday;
                if (obj.du_friend > 0 || obj.hide_city > 1)
                    result.du_city = obj.du_city;
                if (obj.du_friend > 0 || obj.hide_hometown > 1)
                    result.du_hometown = obj.du_hometown;
            }
            return result;
        }
        /// <summary>
        ///  获取我的人脉
        /// </summary>
        /// <param name="param">查询参数</param>
        /// <param name="user">当前登录对象信息</param>
        /// <returns></returns>
        public List<OutMyConnection> GetMyConnection(QueryMyConnection param, DD_User user)
        {
            var list = DBSession.IDD_FriendApplyDAL.GetMyConnection(param, user);
            list?.ForEach(s =>
            {
                if (!string.IsNullOrEmpty(s.user_img) && s.user_img.IndexOf("http") < 0)
                    s.user_img = s.user_img.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                if (!string.IsNullOrEmpty(s.bridge_img))
                    s.bridge_img = s.bridge_img.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                //音频服务地址
                if (!string.IsNullOrEmpty(s.voice_url))
                    s.voice_url = s.voice_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
            });
            return list;
        }
        /// <summary>
        ///  获取人脉列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public List<ConnectionListOutput> GetConnectionList(ConnectionListInput param, int user_id)
        {
            var list = DBSession.IDD_FriendApplyDAL.GetConnectionList(param, user_id);
            list?.ForEach(s =>
            {
                if (s.fileID > 0)
                {
                    var folderId = DBSession.IT_DocumentFolderRelationDAL.FindSingle(o => o.file_id == s.fileID);
                    if (folderId != null)
                    {
                        s.folderList = DBSession.IT_VRStayUserLogDAL.GetFloderAndParent(folderId.folder_id);
                        s.folderList.ForEach(o =>
                        {
                            if(!string.IsNullOrEmpty(o.folderImage))
                            o.folderImage = o.folderImage.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                        });
                    }
                }
                if (!string.IsNullOrEmpty(s.userImg))
                    s.userImg = s.userImg.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
            });
            return list;
        }
        /// <summary>
        ///  获取密友类别
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public List<OutputMyCloseType> GetMyCloseType(int user_id)
        {
            return DBSession.IDD_FriendApplyDAL.GetMyCloseType(user_id);
        }
        /// <summary>
        ///  更新度度圈二度、三度人脉
        /// </summary>
        public void UpdateDD2(int dd_id1, int dd_id2)
        {
            DBSession.IDD_FriendApplyDAL.UpdateDD2(dd_id1, dd_id2);
            DBSession.IDD_FriendApplyDAL.UpdateDD3(dd_id1, dd_id2);
        }
    }
}